package com.example.netmediaplayer;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener,
		OnBufferingUpdateListener, OnPreparedListener {

	private MediaPlayer mMediaPlayer;
	private Button mStartBtn;
	private Button mStopBtn;
	private SeekBar mSeekBar;

	private String mPath = "http://192.168.1.102/letitgo.mp3";
	private Timer mTimer;

	private TimerTask task = new TimerTask() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			
			if(mMediaPlayer == null){
				return;
			}
			
			if(mMediaPlayer.isPlaying() &&(mSeekBar.isPressed()) != true){
				
				mHandler.sendEmptyMessage(0);
			}		
		}
	};

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);

			int position = mMediaPlayer.getCurrentPosition();
			int duration = mMediaPlayer.getDuration();

			if (duration > 0) {
				long pos = mSeekBar.getMax() * position / duration;
				mSeekBar.setProgress((int)pos);
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initWidget();
	}

	private void initWidget() {

		mMediaPlayer = new MediaPlayer();

		mStartBtn = (Button) findViewById(R.id.button1);
		mStopBtn = (Button) findViewById(R.id.button2);
		mSeekBar = (SeekBar) findViewById(R.id.seekBar1);

		mStartBtn.setOnClickListener(this);
		mStopBtn.setOnClickListener(this);

		mMediaPlayer.setOnBufferingUpdateListener(this);
		mMediaPlayer.setOnPreparedListener(this);

		mSeekBar.setMax(100);
		mSeekBar.setOnSeekBarChangeListener(new SeekBarChangeEvent());

		mTimer = new Timer();
		mTimer.schedule(task, 0, 1000); //每隔一秒调用一次taskß
	}
	
	private class SeekBarChangeEvent implements OnSeekBarChangeListener{

		private long progress;
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// TODO Auto-generated method stub

			this.progress = progress * mMediaPlayer.getDuration() / seekBar.getMax();				
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
			mMediaPlayer.seekTo((int)progress);
		}
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		int key = v.getId();

		switch (key) {
		case R.id.button1: // start

			if (mMediaPlayer != null) {
				initMediaPlayer();
			}

			break;
		case R.id.button2: // stop
			if (mMediaPlayer != null) {
				mMediaPlayer.stop();
			}

			break;

		default:
			break;
		}
	}

	/**
	 * 
	 * 初始化MediaPlayer
	 * 
	 */
	private void initMediaPlayer() {

		try {
			mMediaPlayer.reset();
			mMediaPlayer.setDataSource(mPath);
			mMediaPlayer.prepareAsync();

		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onBufferingUpdate(MediaPlayer mp, int percent) {
		// TODO Auto-generated method stub

		mSeekBar.setSecondaryProgress(percent);
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		// TODO Auto-generated method stub

		// 当MediaPlayer准备好以后，自动播放
		mp.start();
		Toast.makeText(this, "MediaPlayer...Start", Toast.LENGTH_SHORT).show();
	}

	/**
	 * 
	 * 当Activity退出后，MediaPlayer退出
	 * 
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

		if (mMediaPlayer != null) {
			mMediaPlayer.release();
			mMediaPlayer = null;
		}
	}

}
